﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kaskelot.Services.Model;

namespace Stockholm.SOA.Kaskelot.WCF.ServiceImplementation.Mapping
{
	public static class MaterielExtensions
	{
		public static DataContracts.Materiel ToContract(this Materiel entity)
		{
			DataContracts.Materiel b = new DataContracts.Materiel();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static DataContracts.Materiel ToMinimalContract(this Materiel entity)
		{
			DataContracts.Materiel b = new DataContracts.Materiel();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static Materiel ToEntity(this DataContracts.Materiel b)
		{
			Materiel entity = new Materiel();

			CopyToEntity(b, entity);

			return entity;
		}

		public static void UpdateEntity(this Materiel entity, DataContracts.Materiel b)
		{
			CopyToEntity(b, entity);
		}

		private static void CopyFromEntity(Materiel from, DataContracts.Materiel to, bool minimal)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;
			to.Antal = from.Antal;

			to.MaterielTypId = from.MaterielTypId;
			to.ByggnadId = from.ByggnadId;
			to.Beskrivning = from.Beskrivning;
			to.MaterielTypBeskrivning = from.MaterielTyp != null ? from.MaterielTyp.Beskrivning : from.Beskrivning;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;
		}

		private static void CopyToEntity(DataContracts.Materiel from, Materiel to)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;
			to.Antal = from.Antal;

			to.Beskrivning = from.Beskrivning;
			to.MaterielTypId = from.MaterielTypId;
			to.ByggnadId = from.ByggnadId;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;
		}
	}
}
